<template>
  <div class="channel">
    <div class="common">
      <div class="common-input">
        <el-input
          v-model="commonValue"
          placeholder="手机号码"
          clearable
        ></el-input>
      </div>

      <div class="common-input">
        <el-date-picker
          v-model="commonDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
           format="yyyy-MM-dd" value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div
        class="common-button"
        style="background: #999"
        @click="replacementHit"
      >
        重置
      </div>
    </div>

    <div class="table" v-loading="loading">
      <el-table
        :data="tableData"
        border
        :header-cell-style="{
          color: '#333333',
          backgroundColor: '#f3f3f3',
        }"
      >
        <el-table-column prop="" label="商品图片" align="center" width="150">
          <template slot-scope="scope">
            <img class="imgs" :src="scope.row.goodIcon" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="goodName" label="商品名称" width="150">
        </el-table-column>
        <el-table-column prop="portrait" label="商品类别" width="300">
          <template slot-scope="scope">
            <el-tag
              >{{ scope.row.goodOneClassifyName }}-{{
                scope.row.goodTwoClassifyName
              }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column prop="goodBrand" label="品牌名称" width="150">
        </el-table-column>
        <el-table-column prop="costPrice" label="商品成本价（元）" width="200">
        </el-table-column>
        <el-table-column prop="retailPrice" label="销售价（元）" width="150">
        </el-table-column>
        <el-table-column prop="shopUserName" label="申请人" width="150">
        </el-table-column>
        <el-table-column
          prop="shopUserPhone"
          label="申请人联系方式"
          width="150"
        >
        </el-table-column>
        <el-table-column prop="shopId" label="申请店铺编号" width="150">
        </el-table-column>
        <el-table-column prop="" label="申请资质" width="250">
          <template slot-scope="scope">
            <div style="display: flex; flex-wrap: wrap">
              <img
                v-for="(item, index) in scope.row.certificationUrlList"
                :key="index"
                class="imgs"
                :src="item"
                alt=""
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="保证金金额" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.earnestMoney == 0">无需保证金</el-tag>
            <el-tag v-else type="success">{{ scope.row.earnestMoney }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="200">
        </el-table-column>
        <el-table-column prop="" label="审核状态" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 0">待审核</el-tag>
            <el-tag v-if="scope.row.status == 1" type="success"
              >审核通过</el-tag
            >
            <el-tag v-if="scope.row.status == 2" type="error">审核失败</el-tag>
            <el-tag v-if="scope.row.status == 3" type="warning"
              >待缴纳保证金</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="applyBreak" label="备注" width="150">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="editHit(scope.row)">查看详情</div>

              <div
                class="list"
                @click.prevent="auditHit(scope.row)"
                v-if="scope.row.status == 0"
              >
                审核
              </div>

              <!-- <div class="list" @click.prevent="deleteHit(scope.row)">删除</div> -->
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="tableDataTotal"
          :current-page="page"
        >
        </el-pagination>
      </div>
    </div>

    <!--新增或者编辑-->
    <div class="add">
      <el-dialog
        title="商品审核"
        :visible.sync="addVisible"
        :before-close="addClose"
        width="30%"
      >
        <div class="add-form">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="demo-ruleForm"
          >
            <div style="display: flex">
              <el-form-item
                label="商品名称:"
                style="width: 50%; margin-bottom: 0"
              >
                {{ commonObj.goodName }}</el-form-item
              >
              <el-form-item
                label="成本价（元）:"
                style="width: 50%; margin-bottom: 0"
                >{{ commonObj.costPrice }}</el-form-item
              >
            </div>

            <div style="display: flex">
              <el-form-item
                label="申请店铺ID:"
                style="width: 50%; margin-bottom: 0"
                >{{ commonObj.shopId }}</el-form-item
              >
              <el-form-item
                label="申请人:"
                style="width: 50%; margin-bottom: 0"
                >{{ commonObj.shopUserName }}</el-form-item
              >
            </div>

            <div style="display: flex">
              <el-form-item
                label="是否缴纳保证金:"
                style="width: 100%; margin-bottom: 0"
                >{{ commonObj.earnestMoney == 0 ? "否" : "是" }}</el-form-item
              >
            </div>

            <div style="display: flex">
              <el-form-item
                label="申请资质:"
                style="width: 100%; margin-bottom: 0"
              >
                <div style="display: flex; flex-wrap: wrap">
                  <img
                    class="img"
                    v-for="(item, index) in commonObj.certificationUrlList"
                    :key="index"
                    :src="item"
                    alt=""
                  />
                </div>
              </el-form-item>
            </div>

            <div style="display: flex">
              <el-form-item
                label="备注:"
                style="width: 100%; margin-bottom: 0"
                >{{ commonObj.applyBreak }}</el-form-item
              >
            </div>

            <div style="display: flex">
              <el-form-item
                label="审核意见:"
                style="width: 100%; margin-bottom: 0"
                prop="text"
                ><el-input
                  type="textarea"
                  :rows="4"
                  placeholder="请输入内容"
                  v-model="ruleForm.text"
                >
                </el-input
              ></el-form-item>
            </div>

            <el-form-item class="attachment">
              <div class="list" @click="addReturn('ruleForm')">不通过</div>
              <div class="list" @click="addConfirm('ruleForm')">通过</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>

    <!--删除弹出框-->
    <div class="delete">
      <el-dialog
        title="删除"
        :visible.sync="deleteVisible"
        :before-close="deleteClose"
      >
        <div class="delete-title">是否确认删除？</div>
        <div class="delete-attachment">
          <div class="list" @click="deleteClose">取消</div>
          <div class="list" @click="deleteConfirm">确定</div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { shopGoodList, shopGoodListAudit } from "@/api/shop";

export default {
  data() {
    return {
      commonValue: "",
      commonDate: [],
      commonStart: "",
      commonEnd: "",
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      dataObj: {},
      loading: false,
      commonObj: {},
      multipleSelection: [],
      //-------------
      addVisible: false,
      addRoaid: 0,
      ruleForm: {
        text: "",
      },
      rules: {
        text: [{ required: true, message: "请输入审核意见", trigger: "blur" }],
      },
      //-----------
      deleteVisible: false,
    };
  },
  created() {
    this.loading = true;

    this.shopGoodListMet();
  },
  methods: {
    shopGoodListMet() {
      let params = {
        keyWord: this.commonValue,
        startTime: this.commonStart,
        endTime: this.commonEnd,
        goodName: "",
        orderType: "",
        orderBy: "",

        pageSize: this.limit,
        pageNo: this.page,
      };

      shopGoodList(params).then((res) => {
        if (res.errorCode == null) {
          this.dataObj = res.resultData;

          res.resultData.goodList.forEach((item, index) => {
            item.retailPrice = Number(item.retailPrice / 100);
            item.costPrice = Number(item.costPrice / 100);
            item.earnestMoney = Number(item.earnestMoney / 100);
            if (item.certificationUrl != null) {
              item.certificationUrlList = item.certificationUrl.split(";");
            } else {
              item.certificationUrlList = [];
            }
          });

          this.tableData = res.resultData.goodList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      });
    },
    timeData(date) {
      return date.toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        // hour: "2-digit",
        // minute: "2-digit",
        // second: "2-digit",
      });
    },
    //-------
    searchHit() {
      if (this.commonDate.length > 0) {
        this.commonStart = this.timeData(new Date(this.commonDate[0]));
        this.commonEnd = this.timeData(new Date(this.commonDate[1]));
      } else {
        this.commonStart = "";
        this.commonEnd = "";
      }

      this.loading = true;
      this.shopGoodListMet();
    },
    replacementHit() {
      this.commonValue = "";
      this.commonDate = [];
      this.commonStart = "";
      this.commonEnd = "";

      this.loading = true;
      this.shopGoodListMet();
    },

    //-----------
    editHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

       this.$router.push({
        path: "/shop/add",
        query: {
          soucre: 1,
          id: item.goodId,
        },
      });
    },
    auditHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      this.commonObj = item;
      this.addVisible = true;
    },
    addClose() {
      this.addVisible = false;
    },
    addReturn(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            shopGoodId: this.commonObj.shopGoodId,
            earnestMoney: Number(this.commonObj.earnestMoney * 100),
            status: 2,
            auditBreak: this.ruleForm.text,
          };
          shopGoodListAudit(params).then((res) => {
            if (res.errorCode == null) {
              this.addVisible = false;
              this.$message.success("审核成功！");
              this.loading = true;
              this.shopGoodListMet();
            }
          });
        } else {
          return false;
        }
      });
    },
    addConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            shopGoodId: this.commonObj.shopGoodId,
            earnestMoney: Number(this.commonObj.earnestMoney * 100),
            status: 1,
            auditBreak: this.ruleForm.text,
          };

          shopGoodListAudit(params).then((res) => {
            if (res.errorCode == null) {
              this.addVisible = false;
              this.$message.success("审核成功！");
              this.loading = true;
              this.shopGoodListMet();
            }
          });
        } else {
          return false;
        }
      });
    },

    //--------
    deleteHit() {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      this.deleteVisible = true;
    },
    deleteClose() {
      this.deleteVisible = false;
    },
    deleteConfirm() {
      let params = this.commonObj.courseId;

      courseListDelete(params).then((res) => {
        if (res.errorCode == null) {
          this.$message.success("删除成功！");
          this.loading = true;
          this.page = 1;
          this.courseListMet();
          this.deleteVisible = false;
        }
      });
    },

    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.shopGoodListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.shopGoodListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/delete.less");

.channel {
  .common {
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    .common-input {
      margin-right: 10px;
      /deep/ .el-input {
        width: 250px;
      }
    }

    .common-input:last-child {
      margin-right: 0;
    }

    .common-button {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      margin-left: 20px;
      cursor: pointer;
    }

    .common-button:last-child {
      background: #999999;
    }

    .common-title {
      margin-left: 130px;
    }
  }

  .table {
    cursor: pointer;

    .operation {
      display: flex;
      align-items: center;
      color: #2a82e4;

      .list {
        margin-right: 20px;
        cursor: pointer;
      }
    }

    .imgs {
      width: 80px;
      height: 80px;
      margin-right: 10px;
    }
  }

  .add {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    /deep/ .el-form-item__label {
      text-align: left;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 70px;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;
        margin-right: 10px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;
        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>